package com.ruoyi.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.app.vo.AppNoticeVo;
import com.ruoyi.business.app.vo.ContactOurVo;
import com.ruoyi.business.app.vo.TitleAndContentVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Api(tags = "通知公告")
@RestController
@RequestMapping("/api/notice")
@AllArgsConstructor
public class NoticeController {


    private final ISysConfigService configService;
    private final SysNoticeMapper noticeMapper;
    private final ISysNoticeService noticeService;

    @ApiOperation("服务协议")
    @GetMapping("/service")
    public AjaxResult<String> service() {
        SysNotice sysNotice = noticeService.getOne(new LambdaQueryWrapper<SysNotice>().select(SysNotice::getNoticeContent).eq(SysNotice::getNoticeType, Constants.FOUR).eq(SysNotice::getStatus, Constants.ZERO));
        return AjaxResult.success("success",sysNotice.getNoticeContent());
    }

    @ApiOperation("隐私政策")
    @GetMapping("/privacy")
    public AjaxResult<String> privacy() {
        SysNotice sysNotice = noticeService.getOne(new LambdaQueryWrapper<SysNotice>().select(SysNotice::getNoticeContent).eq(SysNotice::getNoticeType, Constants.FIVE).eq(SysNotice::getStatus,Constants.ZERO));
        return AjaxResult.success("success",sysNotice.getNoticeContent());
    }

    @ApiOperation("联系我们")
    @GetMapping("/contactour")
    public AjaxResult<ContactOurVo> contactour() {
        ContactOurVo vo = new ContactOurVo();
        //从系统配置表中取得
        LambdaQueryWrapper query = new LambdaQueryWrapper<SysConfig>().select(SysConfig::getConfigKey,SysConfig::getConfigValue)
                .in(SysConfig::getConfigKey,Constants.OFFICIAL_PICURL, Constants.OFFICIAL_WECHAT,Constants.OFFICIAL_PHONE);
        List<SysConfig> list = configService.list(query);
        Map<String, String> maps = list.stream().collect(Collectors.toMap(SysConfig::getConfigKey, SysConfig::getConfigValue, (e1, e2) -> e2));
        vo.setPicUrl(maps.get(Constants.OFFICIAL_PICURL));
        vo.setOfficialWeChat(maps.get(Constants.OFFICIAL_WECHAT));
        vo.setOfficialPhone(maps.get(Constants.OFFICIAL_PHONE));

        return AjaxResult.success(vo);
    }


    @ApiOperation(value = "系统公告")
    @GetMapping("/notice/list")
    public TableDataInfo<TitleAndContentVo> noticeList(@ApiParam(value = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                                                       @ApiParam(value = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<SysNotice> page = noticeMapper.selectPage(new Page(pageNum,pageSize),new LambdaQueryWrapper<SysNotice>()
                .eq(SysNotice::getNoticeType, Constants.SIX).eq(SysNotice::getStatus,Constants.ZERO).orderByDesc(SysNotice::getOrderSort));
        List<AppNoticeVo> vos = new ArrayList<>();
        for (SysNotice sysNotice : page.getRecords()) {
            AppNoticeVo vo = new AppNoticeVo();
            vo.setId(sysNotice.getNoticeId());
            vo.setNoticeTitle(sysNotice.getNoticeTitle());
            vo.setNoticeContent(sysNotice.getNoticeContent());
            vos.add(vo);
        }
        TableDataInfo tb = new TableDataInfo(vos,page.getTotal());
        tb.setCode(AjaxResult.success().getCode());
        tb.setMsg(AjaxResult.success().getMsg());
        return tb;
    }


    @ApiOperation("通知公告详情")
    @GetMapping("/notice/detail")
    public AjaxResult<AppNoticeVo> noticedetail(@ApiParam(value = "通告公告id") @RequestParam Long noticeId) {
        AppNoticeVo appNoticeVo = noticeService.getVoOne(new LambdaQueryWrapper<SysNotice>().eq(SysNotice::getNoticeId, noticeId).eq(SysNotice::getStatus,Constants.ZERO),AppNoticeVo.class);
        return AjaxResult.success(appNoticeVo);
    }
}
